import { _decorator, Button, Label, tween, Tween, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import { ClientAPI } from '../../api/client.api';
import Config from '../../manager/config.manager';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
import GameManager from '../../manager/game.manager';
import type { Popup } from '../page/popup';
const { ccclass, property } = _decorator;

type Fruit = Awaited<ReturnType<typeof GameManager.fusionGet>>;

@ccclass('FruitSpecial')
export class FruitSpecial extends BasePopUp<typeof Popup, boolean> {
	protected default_return: boolean;

	private light_right: UITransform;
	private light_left: UITransform;
	private lbl_symbol: Label;
	private lbl_diamond: Label;
	private icon_symbol: UITransform;
	private icon_diamond: UITransform;

	private symbol: number;
	private diamond: number;
	private logId: number;

	protected initAfterOnLoad(){
		[this.light_left, this.light_right] = this.node.children.filter(n=>n.name.startsWith('light')).map(n=>n.getComponent(UITransform));
		[this.lbl_symbol, this.lbl_diamond] = ['symbol', 'diamond'].map(name=>this.node.getChildByName('lbl-'+name).getComponent(Label));
		[this.icon_symbol, this.icon_diamond] = ['symbol', 'diamond'].map(name=>this.node.getChildByName('icon-'+name).getComponent(UITransform));

		this.renderLocalUI();
	}

	protected _click_event_: { [name: string]: (this: FruitSpecial, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			this.exit(false).then(()=>btn.interactable = true);
		},
		video(btn){
			btn.interactable = false;
			this.exit(true).then(()=>btn.interactable = true);
		},
	}

	public onShow(fruit: Fruit){
		this.symbol = fruit.amount;
		this.diamond = fruit.diamond;
		this.logId = fruit.logId;

		GameDC.Trigger.HILIT_SYMBOL = true;
		GameDC.Trigger.HILIT_DIAMOND = true;

		this.lbl_diamond.string = this.diamond.toString();
		this.lbl_symbol.string = Config.symbolString(this.symbol);

		BasePopUp.jelly_enter(this.node);
		this.enter();
	}

	private enter(){
		[this.light_right, this.light_left].forEach(ui=>{
			Tween.stopAllByTarget(ui.node);

			tween(ui.node)
			.set({angle: 0}).to(5, {angle: -360}).union()
			.repeatForever().start();
		});
	}

	private async exit(reap: boolean){
		if(reap){
			await ClientAPI.AD('incentive');
			GameManager.fusionConfirm(this.logId);
			if(this.symbol){
				GameManager.reapSymbol(this.symbol, this.icon_symbol.node.worldPosition);
			}
			if(this.diamond){
				GameManager.reapDiamond(this.diamond, this.icon_diamond.node.worldPosition);
			}
		}

		await BasePopUp.jelly_leave(this.node);
		this.close(reap);
		[this.light_right, this.light_left].forEach(ui=> Tween.stopAllByTarget(ui.node) );

		GameDC.Trigger.HILIT_SYMBOL = false;
		GameDC.Trigger.HILIT_DIAMOND = false;
	}

	private renderLocalUI(){
		(<const>[
			['lbl-head', 16],
			['lbl-video', 17],
			['close', 18]
		]).forEach(([name, idx])=>this.node.getChildByName(name).getComponent(Label).string = GameAssets.getLangText(idx));

		mtec.cc.skinPeeler(this.icon_symbol.node, GameAssets.getCounTexture(5), {height: true});
	}
}

